package com.gin.kotlin.no_gay_alliance.provider.ngaphp

import com.gin.kotlin.no_gay_alliance.argument.PrivateMessageReplyArgument
import com.gin.nga.main.NgaApi
import com.gin.nga.response.base.SingleData
import com.gin.nga.response.message.PriMsgReplyData
import java.lang.ref.WeakReference

class PrivateMessageReplyProvider private constructor() : AbstractCachedDataProvider<PrivateMessageReplyArgument, SingleData<PriMsgReplyData>, PriMsgReplyData>(
    javaType = TYPE_FACTORY.constructType(PriMsgReplyData::class.java)
) {
    override val dir = "PRIVATE_MESSAGE_REPLY"

    override fun transform(rawData: SingleData<PriMsgReplyData>) = rawData.data ?: PriMsgReplyData()
    override fun requestMethod(ngaApi: NgaApi, params: PrivateMessageReplyArgument?) = ngaApi.messageApi.read(params!!.messageId, params.page ?: 1)
    override fun obtainRelativePath(userId: Long, params: PrivateMessageReplyArgument?) = "${params?.messageId}_${params?.page}.json"

    companion object {
        private var instance: WeakReference<PrivateMessageReplyProvider> = WeakReference(PrivateMessageReplyProvider())
        fun getInstance(): PrivateMessageReplyProvider = instance.get() ?: PrivateMessageReplyProvider().also { instance = WeakReference(it) }
    }
}